﻿// Genius
// Copyright (C) 2016 Dirk Friedenberger, www.frittenburger.de
//   
// This source file is subject to the Microsoft Public License (Ms-PL).
// http://www.opensource.org/licenses/ms-pl.html
// All other rights reserved.
using Genius.Brain.Shared;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Genius.Brain.Shared.BO;
using Genius.Brain.Shared.Locator;

namespace Genius.Brain.Impl
{
    class Genius : IGenius
    {
        public string Id { get; set; }

        public string Name { get; set; }

        public IList<IChannel> Channels { get; set; }

        public IList<IInstantMessaging> IMs { get; set; }
        public IList<IGeniusModule> Modules { get; set; }

        private IList<IDialog> _dialogs = new List<IDialog>();


        public IMessage RecvMessage(IMessage message)
        {
            if (Id.Equals(message.User)) return null; //Own Message

            Console.WriteLine("Recv Message " + message.Text + " from " + message.User);


            var response = BrainLocator.GetMessage();
            response.User = Id;
            response.Text = "";
            response.ChannelId = message.ChannelId;


            var dialog = _dialogs.SingleOrDefault(d => d.Channel == message.ChannelId && d.User == message.User);
            if(dialog == null)
            {
                if (IMs.Where(im => im.Id == message.ChannelId).Count() == 0)
                {
                    if(!message.Text.Contains("<@"+Id+">")
                        && !message.Text.Contains("<@" + Id + "|"))
                        return null; //nur direkte Messages

                    if (Channels.Where(ch => ch.Id == message.ChannelId).Count() == 0)
                        return null; //Unknown Channel
                }

                dialog = BrainLocator.GetDialog();
                dialog.Channel = message.ChannelId;
                dialog.User = message.User;
                dialog.Context = BrainLocator.GetDialogContext();
                dialog.Context.Name = Name;
                dialog.Module = null;

                _dialogs.Add(dialog);
            }

            var TextParserStrategy = BrainLocator.GetTextParserStrategy( @"Data\genius.json");
            var TextFormaterStrategy = BrainLocator.GetTextFormaterStrategy( @"Data\genius.json");

            var Text = TextParserStrategy.Parse(message.Text);

            if (Text.Token.Equals("#STOPDIALOG"))
            {
                //Dialog Abbruch
                _dialogs.Remove(dialog);
                response.Text = TextFormaterStrategy.Format(BrainLocator.GetText("#CONFIRM", Text.Language));
                return response;
            }

            //Normale Verarbeitung
            if (dialog.Module != null)
            {
                switch(dialog.Module.RecvMessage(dialog.Context, message, response))
                {
                    case GeniusModuleState.Cont: 
                        break;
                    case GeniusModuleState.None: //eigentlich unplausibel
                    case GeniusModuleState.Once: //Letzer Aufruf
                        dialog.Module = null; //Modul Ende
                        break;
                }
            }
            else
            {
                foreach (var module in Modules)
                {
                    switch (module.RecvMessage(dialog.Context, message, response))
                    {
                        case GeniusModuleState.Cont:
                            dialog.Module = module;  //Modul Start
                            break;
                        case GeniusModuleState.Once:
                            //Einmaliger Aufruf
                            break;
                        case GeniusModuleState.None:
                            continue;
                    }
                    break;
                }
            }

            if (String.IsNullOrWhiteSpace(response.Text))
            {
                response.Text = TextFormaterStrategy.Format(BrainLocator.GetText("#DONTUNDERSTAND", "de"));
            }

            Console.WriteLine("Send Message " + response.Text + " from " + response.User);
            return response;


        }
    }
}
